/**
 * @file initDetail.js
 * @author zhangLuPing <zhangluping@antiy.cn>
 * @createTime 2019-03-01 09:52:54
 * @description 日志详情数据初始化，为减轻详情页的判断 & 处理
 */

import countryAbbMap from '../../../utils/maps/countryAbbMap'

class SafeEventDetail {
  constructor ({
    // 公共字段
    id,
    rTime,
    stype,
    ts,
    level,
    sip,
    sport,
    dip,
    dport,
    proto,
    desc,
    host,
    md5,
    malname,
    status,
    node,
    action,
    url,
    // WAF
    rawMsg,
    tag,
    // 防火墙检测
    policy,
    send,
    recvd,
    szone,
    dzone,
    // web应用防火墙
    clientEnv,
    // 入侵检测
    // 入侵防御
    _break,
    // 恶意代码检测
    filename,
    behavior,
    virusFamily,
    virusType,
    // C&C通讯
    port,
    msg,
    // 载荷深度分析
    isMalicious,
    fileName,
    fileType,
    fileExt,
    fileMd5,
    fileBytes,
    dangerBehavior,
    fileMonitor,
    networkMonitor,
    // 恶意代码传输
    // 脆弱性
    cveId,
    cncveId,
    vulName,
    vulType,
    affect,
    solution,
    // 系统漏洞
    affectSoftwares,
    serverName,
    // web应用漏洞
    // 审计
    clientIp,
    user,
    roles,
    result,
    logSource,
    logObject,
    // 违规外联
    // 违规接入
    devName,
    devId,
    op,
    // 系统/设备运行状态
    uptime,
    runtime,
    sysName,
    sysId,
    // 系统节点信息
    nodeInfoVisible,
    nodeInfoName,
    nodeInfoCode,
    nodeInfoNodeType,
    nodeInfoOperateSystem,
    nodeInfoAgencyName,
    nodeInfoLevel,
    nodeInfoMaintainer,
    nodeInfoUptime,
    nodeInfoMaintainerPhone,
    nodeInfoMaintainerEmail,
    nodeInfoSysNodeDes,
    nodeInfoTag,
    nodeInfoDns,
    nodeInfoIsRegister,
    // 系统信息
    sysInfoVisible,
    sysInfoName,
    sysInfoDesc,
    sysInfoCode,
    sysInfoUrl,
    sysInfoBelongArea,
    sysInfoUnit,
    sysInfoUptime,
    sysInfoMaintainer,
    sysInfoTag,
    sysInfoRemark,
    sysInfoMaintainerPhone,
    sysInfoMaintainerEmail
  }) {
    // 公共字段
    this.id = id
    this.rTime = rTime
    this.stype = stype
    this.ts = ts
    this.level = level
    this.sip = sip
    this.sport = sport
    this.dip = dip
    this.dport = dport
    this.proto = proto
    this.desc = desc
    this.host = host
    this.md5 = md5
    this.malname = malname
    this.status = status
    this.node = node
    this.action = action
    this.url = url
    // WAF
    this.rawMsg = rawMsg
    this.tag = tag
    this.policy = policy
    this.send = send
    this.recvd = recvd
    this.szone = szone
    this.dzone = dzone
    // web应用防火墙
    this.clientEnv = clientEnv
    // 入侵检测
    // 入侵防御
    this._break = _break
    // 恶意代码检测
    this.filename = filename
    this.behavior = behavior
    this.virusFamily = virusFamily
    this.virusType = virusType
    // C&C通讯
    this.port = port
    this.msg = msg
    // 载荷深度分析
    this.isMalicious = isMalicious
    this.fileName = fileName
    this.fileType = fileType
    this.fileExt = fileExt
    this.fileMd5 = fileMd5
    this.fileBytes = fileBytes
    this.dangerBehavior = dangerBehavior
    this.fileMonitor = fileMonitor
    this.networkMonitor = networkMonitor
    // 恶意代码传输
    // 脆弱性
    this.cveId = cveId
    this.cncveId = cncveId
    this.vulName = vulName
    this.vulType = vulType
    this.affect = affect
    this.solution = solution
    // 系统漏洞
    this.affectSoftwares = affectSoftwares
    this.serverName = serverName
    // web应用漏洞
    // 审计
    this.clientIp = clientIp
    this.user = user
    this.roles = roles
    this.result = result
    this.logSource = logSource
    this.logObject = logObject
    // 违规外联
    // 违规接入
    this.devName = devName
    this.devId = devId
    this.op = op
    // 系统/设备运行状态
    this.uptime = uptime
    this.runtime = runtime
    this.sysName = sysName
    this.sysId = sysId
    // 系统节点信息
    this.nodeInfoName = nodeInfoName
    this.nodeInfoCode = nodeInfoCode
    this.nodeInfoNodeType = nodeInfoNodeType
    this.nodeInfoOperateSystem = nodeInfoOperateSystem
    this.nodeInfoAgencyName = nodeInfoAgencyName
    this.nodeInfoLevel = nodeInfoLevel
    this.nodeInfoMaintainer = nodeInfoMaintainer
    this.nodeInfoUptime = nodeInfoUptime
    this.nodeInfoIsRegister = nodeInfoIsRegister
    this.nodeInfoMaintainerPhone = nodeInfoMaintainerPhone
    this.nodeInfoMaintainerEmail = nodeInfoMaintainerEmail
    this.nodeInfoSysNodeDes = nodeInfoSysNodeDes
    this.nodeInfoTag = nodeInfoTag
    this.nodeInfoDns = nodeInfoDns
    this.nodeInfoVisible = nodeInfoVisible
    // 系统信息
    this.sysInfoVisible = sysInfoVisible
    this.sysInfoName = sysInfoName
    this.sysInfoDesc = sysInfoDesc
    this.sysInfoCode = sysInfoCode
    this.sysInfoUrl = sysInfoUrl
    this.sysInfoBelongArea = sysInfoBelongArea
    this.sysInfoUnit = sysInfoUnit
    this.sysInfoUptime = sysInfoUptime
    this.sysInfoMaintainer = sysInfoMaintainer
    this.sysInfoTag = sysInfoTag
    this.sysInfoRemark = sysInfoRemark
    this.sysInfoMaintainerPhone = sysInfoMaintainerPhone
    this.sysInfoMaintainerEmail = sysInfoMaintainerEmail
  }
}

/**
 * @description 判断属性是否存在
 * @param {any} obj
 * @returns {Boolean}
 */
const isNone = obj => {
  let _type = Object.prototype.toString.call(obj).slice(8, -1)
  return _type === 'Undefined' || _type === 'Null'
}

/**
 * @description 生成国家对应的国旗class名称
 * @param {String} country
 * @returns {String}
 */
const transAbb = country => {
  if (countryAbbMap[country] !== undefined && countryAbbMap[country].length !== 0) {
    return `flag-icon-${countryAbbMap[country]}`
  } else {
    return ''
  }
}

export const initDetail = rawData => {
  // 深复制
  let data = JSON.parse(JSON.stringify(rawData))
  const visible = 1
  const notVisible = 0
  data.sysInfoVisible = isNone(data.sys_info) ? notVisible : visible
  data.sys_info = isNone(data.sys_info) ? {} : data.sys_info
  data.file = isNone(data.file) ? {} : data.file
  data.nodeInfoVisible = isNone(data.node_info) ? notVisible : visible
  data.node_info = isNone(data.node_info) ? {} : data.node_info
  data.action = isNone(data.action) ? '' : (data.action === 'deny' ? '拒绝' : '允许')
  // 数组 =》 以逗号分割字符串
  data.behavior = !isNone(data.behavior) && data.behavior.length !== 0 ? data.behavior.map((val, index) => { return `${index + 1}、${val}` }).join('') : ''
  data.tag = !isNone(data.tag) && data.tag.length !== 0 ? data.tag.map((val, index) => { return `${index + 1}、${val}` }).join('') : ''
  data.affect = !isNone(data.affect) && data.affect.length !== 0 ? data.affect.map((val, index) => { return `${index + 1}、${val}` }).join('') : ''
  data.affect_softwares = !isNone(data.affect_softwares) && data.affect_softwares.length !== 0 ? data.affect_softwares.map((val, index) => { return `${index + 1}、${val}` }).join('') : ''

  return new SafeEventDetail({
    // 公共字段
    id: data.id || '',
    rTime: data.r_time || '',
    stype: data.stype || '',
    ts: data.ts || '',
    level: data.level || '',
    // 系统信息
    sysInfoVisible: data.sysInfoVisible || '',
    sysInfoName: data.sys_info.name || '',
    sysInfoDesc: data.sys_info.desc || '',
    sysInfoCode: data.sys_info.code || '',
    sysInfoUrl: data.sys_info.url || '',
    sysInfoBelongArea: data.sys_info.belong_area || '',
    sysInfoUnit: data.sys_info.unit || '',
    sysInfoUptime: data.sys_info.uptime || '',
    sysInfoMaintainer: data.sys_info.maintainer || '',
    sysInfoTag: data.sys_info.tag || '',
    sysInfoRemark: data.sys_info.remark || '',
    sysInfoMaintainerPhone: data.sys_info.maintainer_phone || '',
    sysInfoMaintainerEmail: data.sys_info.maintainer_email || '',
    // 系统节点信息
    nodeInfoVisible: data.nodeInfoVisible || '',
    nodeInfoName: data.node_info.name || '',
    nodeInfoCode: data.node_info.code || '',
    nodeInfoNodeType: data.node_info.node_type || '',
    nodeInfoOperateSystem: data.node_info.operate_system || '',
    nodeInfoAgencyName: data.node_info.agency_name || '',
    nodeInfoLevel: data.node_info.level || '',
    nodeInfoMaintainer: data.node_info.maintainer || '',
    nodeInfoUptime: data.node_info.uptime || '',
    nodeInfoIsRegister: data.node_info.is_register || '',
    nodeInfoMaintainerPhone: data.node_info.maintainer_phone || '',
    nodeInfoMaintainerEmail: data.node_info.maintainer_email || '',
    nodeInfoSysNodeDes: data.node_info.sys_node_des || '',
    nodeInfoTag: data.node_info.tag || '',
    nodeInfoDns: data.node_info.dns || '',
    sip: data.sip || '',
    sport: data.sport || '',
    dip: data.dip || '',
    dport: data.dport || '',
    proto: data.proto || '',
    desc: data.desc || '',
    host: data.host || '',
    md5: data.md5 || '',
    malname: data.malname || '',
    status: data.status || '',
    node: data.node || '',
    action: data.action || '',
    url: data.url || '',
    // WAF
    rawMsg: data.raw_msg || '',
    tag: data.tag || '',
    policy: data.policy || '',
    send: data.send || '',
    recvd: data.recvd || '',
    szone: data.szone || '',
    dzone: data.dzone || '',
    // web应用防火墙
    clientEnv: data.client_env || '',
    // 入侵检测
    // 入侵防御
    _break: data.break || '',
    // 恶意代码检测
    filename: data.filename || '',
    behavior: data.behavior || '',
    virusFamily: data.virus_family || '',
    virusType: data.virus_type || '',
    // C&C通讯
    port: data.port || '',
    msg: data.msg || '',
    // 载荷深度分析
    isMalicious: data.is_malicious || '',
    fileName: data.file.name || '',
    fileType: data.file.type || '',
    fileExt: data.file.ext || '',
    fileMd5: data.file.md5 || '',
    fileBytes: data.file.bytes || '',
    dangerBehavior: data.danger_behavior || '',
    fileMonitor: data.file_monitor || '',
    networkMonitor: data.network_monitor || '',
    // 恶意代码传输
    // 脆弱性
    cveId: data.cve_id || '',
    cncveId: data.cncve_id || '',
    vulName: data.vul_name || '',
    vulType: data.vul_type || '',
    affect: data.affect || '',
    solution: data.solution || '',
    // 系统漏洞
    affectSoftwares: data.affect_softwares || '',
    serverName: data.server_name || '',
    // web应用漏洞
    // 审计
    clientIp: data.client_ip || '',
    user: data.user || '',
    roles: data.roles || '',
    result: data.result || '',
    logSource: data.log_source || '',
    logObject: data.log_object || '',
    // 违规外联
    // 违规接入
    devName: data.dev_name || '',
    devId: data.dev_id || '',
    op: data.op || '',
    // 系统/设备运行状态
    uptime: data.uptime || '',
    runtime: data.runtime,
    sysName: data.sys_name || '',
    sysId: data.sys_id || ''
  })
}
